﻿
/// <summary>

/// Builds a page list.
/// TODO: Update and add more comments ///

/// </summary>

namespace App_Code.Controls   ///Todo: Note this is new for controls ///
{

    using System;
    using System.IO;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;  ///Todo:  Needed look at pagelist ///
    
    /// Do not know which ones below are needed?  ///
    using System.Collections.Specialized; /// For sure needed ///
    using System.Web;
    using System.Web.Security;
    using BlogEngine.Core;


 
   

    public class LinkMenu : Control
    {
        /// <summary>

        /// Initializes the <see cref="PageList"/> class.

        /// </summary>

        static LinkMenu()
        {
            BlogEngine.Core.Page.Saved += delegate { _Html = null; };
        }
        #region Properties

        private string _Filter;
        public string Filter
        {
            get { return _Filter; }
            set { _Filter = value; }
        }
        private static object _SyncRoot = new object();
        private static NameValueCollection _Html;
        /// <summary>

        /// Caches the rendered HTML in the private field and first

        /// updates it when a post has been saved (new or updated).

        /// </summary>

        private NameValueCollection Html
        {
            get
            {
                if (_Html == null ||
                    (!String.IsNullOrEmpty(Filter) && _Html[Filter] == null))
                {
                    lock (_SyncRoot)
                    {
                        if (_Html == null ||
                            (!String.IsNullOrEmpty(Filter) && _Html[Filter] == null))
                        {
                            if (_Html == null)
                                _Html = new NameValueCollection();
                            HtmlGenericControl ul = BindPages();
                            System.IO.StringWriter sw = new System.IO.StringWriter();
                            ul.RenderControl(new HtmlTextWriter(sw));
                            _Html.Add(Filter, sw.ToString());
                        }
                    }
                }
                return _Html;
            }
        }
        #endregion
        /// <summary>

        /// Loops through all pages and builds the HTML

        /// presentation.

        /// </summary>

        /// TODO: Fix the <b> code Need little inpack on the html Just need li thats it ///
     
        private HtmlGenericControl BindPages()
        {

            HtmlGenericControl ul = new HtmlGenericControl("b");
             
    
			/// Makes sure its Listed and Published ///

///foreach (var page in BlogEngine.Core.Page.Pages.Where(page => page.ShowInList && page.IsVisibleToPublic 
               /// )) ///

            foreach (var page in BlogEngine.Core.Page.Pages.OrderBy(page => page.Pagesort))

			
			/// Filters by Link Name ///
                if (String.IsNullOrEmpty(Filter) || page.Linkmenu.ToLower().StartsWith(_Filter.ToLower()))


                    if (page.ShowInList && page.IsPublished)
                    {

              
                    HtmlGenericControl li = new HtmlGenericControl("li");
                    HtmlAnchor anc = new HtmlAnchor();
                    
					anc.HRef = page.RelativeLink.ToString();
                    anc.InnerHtml = page.Linkname;    // PageLinkMenu Step 20 Uses LinkName instead of title
                    anc.Title = page.Description;
                    
					li.Controls.Add(anc);

                    ul.Controls.Add(li);
                    }

            return ul;  
        }
        /// <summary>

        /// Renders the control.

        /// </summary>

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.Write(Html[Filter]);
            writer.Write(Environment.NewLine);
        }
    }
}
